package org.xinjiang.tarim.travel.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xinjiang.tarim.travel.mapper.PoiMddMapper;
import org.xinjiang.tarim.travel.model.LookMddRecord;
import org.xinjiang.tarim.travel.model.PoiMddWithBLOBs;
import org.xinjiang.tarim.travel.service.AttentionMddService;
import org.xinjiang.tarim.travel.service.LookMddRecordService;
import org.xinjiang.tarim.travel.service.PoiMddService;
import org.xinjiang.tarim.travel.service.PoiService;
import org.xinjiang.tarim.travel.utils.Constants;
import org.xinjiang.tarim.travel.utils.JsonResult;
import org.xinjiang.tarim.travel.vo.Criteria;
import org.xinjiang.tarim.travel.vo.PageBean;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PoiMddServiceImpl implements PoiMddService {
    @Autowired
    PoiMddMapper poiMddMapper;
    @Autowired
    LookMddRecordService lookMddRecordService;
    @Autowired
    AttentionMddService attentionMddService;
    @Autowired
    PoiService poiService;

    @Override
    public Long selectByConditionCount(Criteria criteria) {
        return poiMddMapper.selectByConditionCount(criteria);
    }

    @Override
    public PageBean<PoiMddWithBLOBs> selectByCondition(Criteria criteria, PageBean pageBean) {
        criteria.addExtField("pageIndex", (pageBean.getPageIndex()-1) * pageBean.getPageSize());
        criteria.addExtField("pageSize", pageBean.getPageSize());
        Long totalCount = poiMddMapper.selectByConditionCount(criteria);
        List list = poiMddMapper.selectByCondition(criteria);
        pageBean.setTotalCount(totalCount);
        pageBean.setResultList(list);
        return pageBean;
    }

    @Override
    public List<PoiMddWithBLOBs> selectAll(Criteria criteria) {
        return poiMddMapper.selectAll(criteria);
    }

    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Override
    public JsonResult selectByMddId(String mddId, String openId) {
        JsonResult jsonResult;
        if(openId != null && openId != ""){
            Criteria criteria = new Criteria();
            criteria.addExtField("mddId", mddId);
            criteria.addExtField("openid", openId);
            List list = lookMddRecordService.selectByConditionWithOutPage(criteria);
            LookMddRecord record = new LookMddRecord();
            record.setMddId(mddId);
            record.setOpenid(openId);
            if(list.size() > 0){
                Date now = new Date();
                record.setModifyTime(now);
                record.setCreateTime(now);
                lookMddRecordService.updateByPrimaryKeySelective(record);
            }else{
                lookMddRecordService.insert(record);
            }
        }
        Map map = new HashMap();
        PoiMddWithBLOBs mdd = poiMddMapper.selectByMddId(mddId);
        if(mdd == null){
            jsonResult = new JsonResult(Constants.STATUS_NOT_FOUND, "查询目的地失败");
        }else {
            List pois = poiService.selectByMddId(mddId);
            Criteria criteria = new Criteria();
            criteria.addExtField("mddId", mddId);
            criteria.addExtField("openid", openId);
            List list = attentionMddService.selectByConditionWithOutPage(criteria);
            map.put("isAttention", list.size()==0?false:true);
            map.put("mdd", mdd);
            map.put("pois", pois);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "查询目的地成功", map);
        }
        return jsonResult;
    }
}
